<?php

/**
 * Description of ToolBox
 *
 * @author bonfiglio
 */
class ToolBox {

    /**
     * Transformer un tableau multidimensionnel, en un tableau a une seule dimension
     * @param type $a
     * @return type 
     */
    public static function array_flatten($a) {
        if (!count($a))
            return array();
        foreach ($a as $k => $v)
            $a[$k] = (array) $v;
        return call_user_func_array('array_merge', $a);
    }

    public static function array_to_csv($datas, $fileNamePath, $fieldSeparator = ";", $endLine = "\r", $quoteData = "",$convertToWindows = false) {
        if (!count($datas)) {
            return false;
        }
        $file = fopen($fileNamePath, "a+");
        if (!$file) {
            return false;
        }
        ftruncate($file, 0);
        foreach ($datas as $data) {
            if (!empty($quoteData)) {
                foreach ($data as $key => $value) {
                    $data[$key] = $quoteData.$value.$quoteData;
                }
            }
            $content = implode($fieldSeparator,$data).$endLine;
            if($convertToWindows){
                $content = iconv("UTF-8", "WINDOWS-1252", $content);
            }
            $witten = Log::fputs($fileNamePath, $content);
            if(!$witten){
                return false;
            }
        }
        return true;
    }
    
    public static function dateReverse($string,$delimiter = '/'){
        return implode($delimiter, array_reverse(explode($delimiter, $string)));
    }

}

?>
